package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Area;
import cn.wolfcode.trip.base.query.HotelQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.AreaQueryObject;
import cn.wolfcode.trip.base.service.IAreaService;
import cn.wolfcode.trip.base.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/area")
public class AreaController {

    @Autowired
    private IAreaService areaService;
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){

        return "/area/list";
    }

    @RequestMapping("getArea")
    @ResponseBody
    public Object getArea(@ModelAttribute("qo") AreaQueryObject qo){
        List<Area> list=areaService.selectAreaByParentId(qo);
        //创建一个Mlist用来存放对应数据的map
        List<Map<String,Object>> mList = new ArrayList<Map<String, Object>>();
        for (Area area : list) {
            Map<String, Object> map = area.changeToMap();
            mList.add(map);
        }
        return mList;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Area area){
        JSONResult result = new JSONResult();
        try{
            areaService.saveOrUpdate(area);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id){
        areaService.deleteById(id);
        return  new JSONResult();
    }


}
